package com.aso114.project.mvp.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.SatPractieAdapter;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.TrueQuestionBean;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SpaceItemDecoration;
import com.aso114.project.util.SystemBarHelper;
import com.aso114.project.util.ToastUtils;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 真题模考
 */
public class SatPracticeTestActivity extends BaseSimpleActivity implements SwipeRefreshLayout.OnRefreshListener, SwipeMenuRecyclerView.LoadMoreListener {


    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout historyRefreshLy;
    @BindView(R.id.practice_test_listView)
    SwipeMenuRecyclerView recyclerView;
    Intent intent;
    List<TrueQuestionBean> mData = new ArrayList<>();
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.layout_title_bar_right_tv)
    TextView layoutTitleBarRightTv;
    @BindView(R.id.null_ly)
    LinearLayout nullLy;
    private SatPractieAdapter recycleAdapter;
    String testType = "";
    String catid = Constant.adutst;
    Dialog dialog;
    //刷新
    private boolean isRefresh = false;

    private int pageNo = 1;

    @Override
    protected int getResouseId() {
        return R.layout.activity_sat_practice_test;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        testType = getIntent().getStringExtra("type");

        if (testType.equals("1")) {
            layoutTitleBarTitleTv.setText("真题模考");

            catid = getIntent().getStringExtra("catid");
        }
        if (testType.equals("30")) {
            layoutTitleBarTitleTv.setText("巩固模考");
        }

        //设置分割线使用的divider
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration
// .VERTICAL));

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.addItemDecoration(new SpaceItemDecoration(30));
//        CustomProgressDialog dialog = new CustomProgressDialog(this,"");
//        dialog.setCanceledOnTouchOutside(true);//设置是否可以点击外部消失
//        final View dialogview = SatPracticeTestActivity.this.getLayoutInflater().inflate(
//                R.layout.loading_view, null);
//        dialog.setContentView(dialogview, new ViewGroup.LayoutParams(
//                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        dialog.setCancelable(true);//设置是否可以按退回键取消
//        dialog.show();

        recycleAdapter = new SatPractieAdapter(SatPracticeTestActivity.this, mData);
        recyclerView.setAdapter(recycleAdapter);
// 刷新
        historyRefreshLy.setOnRefreshListener(this);
        // 加载
        recyclerView.useDefaultLoadMore();
        recyclerView.setLoadMoreListener(this);
        isRefresh = true;
        getData(pageNo, testType, catid);


        //这里使用线性布局像listview那样展示列表,第二个参数可以改为 HORIZONTAL实现水平展示
//        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//        recycleview.setLayoutManager(linearLayoutManager);
        //使用网格布局展示
//        recycleview.setLayoutManager(new GridLayoutManager(this, 5));


    }


    @Override
    public void onRefresh() {
        // 刷新
        // 下拉刷新
        pageNo = 1;
        isRefresh = true;
        getData(pageNo, testType, catid);
    }

    @Override
    public void onLoadMore() {
        // 加载
        // 上拉加载
        pageNo++;
        isRefresh = false;
        getData(pageNo, testType, catid);
    }


    public void  getData(int page, String testType, final String catid) {
        if (isRefresh) {
            historyRefreshLy.setRefreshing(true);
        }

        CommentModel.getInstant().get_true_quewtion(page, testType, catid, new CallBack() {
            @Override
            public void onSuccess(Object o, boolean isSuccess, String msg) {
                Helper.gotoLogin(isSuccess);//token失效去重新登陆

                List<TrueQuestionBean> Data = (List<TrueQuestionBean>) o;

//                 string wuxiao = ((List<TrueQuestionBean>) o).get(0)

                System.out.println("真题模考数组大小===" + Data.size());

                if (historyRefreshLy == null) {
                    return;
                }
                if (isRefresh) {
                    pageNo = 1;
                    historyRefreshLy.setRefreshing(false);
                    mData.clear();
                }

                if (Data == null || Data.size() == 0) {
                    if (pageNo == 1) {
                        nullLy.setVisibility(View.VISIBLE);
                        historyRefreshLy.setVisibility(View.GONE);
                        recyclerView.loadMoreFinish(true, true);
                        recycleAdapter.notifyDataSetChanged();
                    } else {


                        recyclerView.loadMoreFinish(true, true);
                        recycleAdapter.notifyDataSetChanged();
                        ToastUtils.showToast(SatPracticeTestActivity.this, "已加载全部数据");
                    }
                } else {
                    nullLy.setVisibility(View.GONE);
                    historyRefreshLy.setVisibility(View.VISIBLE);
                    mData.addAll(Data);
                    recycleAdapter.notifyDataSetChanged();
                    recyclerView.loadMoreFinish(false, true);
//                    recycleAdapter = new SatPractieAdapter(SatPracticeTestActivity.this, mData);
//                    //设置Adapter
//                    recyclerView.setAdapter(recycleAdapter);
                    //设置增加或删除条目的动画
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
                    //设置边距
//                recyclerView.addItemDecoration(new SpaceItemDecoration(0, 10));

                    recycleAdapter.setOnItemClickListener(new SatPractieAdapter.OnItemClickListener() {

                        @Override
                        public void onItemClick(View view, int postion) {
//                        ToastUtils.showToast(SatPracticeTestActivity.this, mData.get(postion)
//                                .getPaperTitle() + "");
                            intent = new Intent(SatPracticeTestActivity.this, SatGuideActivity.class);
//                        Bundle mBundle = new Bundle();
//                        mBundle.putSerializable("data",(Serializable)mData);
//                        intent.putExtras(mBundle);
                            intent.putExtra("data", (Serializable) mData.get(postion));
                            intent.putExtra("catid", catid);
                            startActivity(intent);
                        }
                    });

                }


            }

            @Override
            public void onFailure(String fail) {
                dialog.cancel();
            }

        });


    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.layout_title_bar_title_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.layout_title_bar_title_tv:
                break;
        }
    }
}
